//
//  MHMHRedPacketOrderRecordItemViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/4.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHRedPacketOrderRecordItemViewModel.h"
#import "NSMutableAttributedString+MHMoment.h"
#import "MHRedHistoryRecord.h"

@implementation MHRedPacketOrderRecordItemViewModel
- (instancetype)initWithModel:(MHRedHistoryRecord * )model{
    if (self = [super init]) {
        self.model = model;
        
        //单行显示
        YYTextContainer *singleRowContainer = [YYTextContainer containerWithSize:YYTextContainerMaxSize];
        singleRowContainer.maximumNumberOfRows = 1;
        
//        NSString *coverUrl = ;
        
        CGFloat coverViewX = MHCommonConentInset13;
        CGFloat coverViewY = MHCommonConentInset15;
        CGFloat coverViewW = MHCommonConentSize72;
        CGFloat coverViewH = MHCommonConentSize72;
        self.coverImageFrame = CGRectMake(coverViewX, coverViewY, coverViewW, coverViewH);
        
        NSString *content = model.content;
        CGFloat limitWidth = MHRedPacketContentRecordViewWidth();
        if(MHStringIsNotEmpty(content))
        {
//            YYTextContainer *contentLableContainer = [YYTextContainer containerWithSize:CGSizeMake(limitWidth, MAXFLOAT)];
//            contentLableContainer.maximumNumberOfRows = 3;
            NSMutableAttributedString *contAttri = [[NSMutableAttributedString alloc] initWithString:content];
            contAttri.yy_font = MHRegularFont_12;
            contAttri.yy_color = MHMainTextColor;
            contAttri.yy_lineBreakMode = NSLineBreakByCharWrapping;
            contAttri.yy_alignment = NSTextAlignmentLeft;
            [contAttri mh_regexContentWithWithEmojiImageFontSize:12];
            
            YYTextLayout *contentLableLayout = [YYTextLayout layoutWithContainerSize:CGSizeMake(limitWidth, MHCommonConentSize51) text:contAttri.copy];
//            contentLableLayout.textBoundingSize = CGSizeMake(limitWidth, 51);
            self.contentLableLayout = contentLableLayout;
        }
        
        NSString *timeAndLocationString = [NSString stringWithFormat:@"%@  %@",model.created,model.circleName];
        if(MHStringIsNotEmpty(timeAndLocationString))
        {
            NSMutableAttributedString *timeAndLocationAttriString = [[NSMutableAttributedString alloc] initWithString:timeAndLocationString];
            timeAndLocationAttriString.yy_font = MHRegularFont_10;
            timeAndLocationAttriString.yy_color = MHMetionTextColor;
            timeAndLocationAttriString.yy_lineBreakMode = NSLineBreakByCharWrapping;
            timeAndLocationAttriString.yy_alignment = NSTextAlignmentLeft;
            YYTextLayout *timeLayout = [YYTextLayout layoutWithContainer:singleRowContainer text:timeAndLocationAttriString.copy];
            self.timeAndSourceLayout = timeLayout;
        }
        
        NSString *amountStirng = (self.model.type == RedPacketHistoryTypeReceive) ?  [NSString stringWithFormat:@"+%.2f",model.amount] : [NSString stringWithFormat:@"-%.2f",model.amount];
        if(MHStringIsNotEmpty(amountStirng))
        {
            NSMutableAttributedString *amountStirngAttriString = [[NSMutableAttributedString alloc] initWithString:amountStirng];
            amountStirngAttriString.yy_font = MHRegularFont_15;
            amountStirngAttriString.yy_color = (self.model.type == RedPacketHistoryTypeReceive) ? MHColorFromHexString(@"#51C332") : MHMainTextColor;
            amountStirngAttriString.yy_lineBreakMode = NSLineBreakByCharWrapping;
            amountStirngAttriString.yy_alignment = NSTextAlignmentLeft;
            YYTextLayout *amoutLayout = [YYTextLayout layoutWithContainer:singleRowContainer text:amountStirngAttriString.copy];
            self.amountLayout = amoutLayout;
        }
        
        
        
        
        
        [self initialize];
    }
    return self;
}

-(CGRect)contentFrame
{
    CGFloat contentLableX = CGRectGetMaxX(self.coverImageFrame) + MHCommonConentInset18;
    CGFloat contentLableY = MHCommonConentInset15;
    return CGRectMake(contentLableX, contentLableY, self.contentLableLayout.textBoundingSize.width, self.contentLableLayout.textBoundingSize.height);
}

-(CGRect)amountFrame
{
    CGFloat amountLableX = MH_SCREEN_WIDTH - self.amountLayout.textBoundingSize.width - MHCommonConentInset18;
    CGFloat amountLableY = CGRectGetMaxY(self.contentFrame) + MHCommonConentInset15 ;
    return CGRectMake(amountLableX, amountLableY, self.amountLayout.textBoundingSize.width, 51);
}

-(CGRect)timeAndSourceFrame
{
    CGFloat timeAndSourceLableX = self.contentFrame.origin.x;
    CGFloat timeAndSourceLableY = CGRectGetMinY(self.amountFrame) + 20 ;
    return CGRectMake(timeAndSourceLableX, timeAndSourceLableY, self.timeAndSourceLayout.textBoundingSize.width, self.timeAndSourceLayout.textBoundingSize.height);
}

- (void)initialize
{
    
}
@end
